<%@ page language="java" contentType="text/html; charset=UTF-8" 
import="org.windblog.core.*,java.util.*,org.windblog.core.entity.*"  pageEncoding="UTF-8"%>
<%
	String mnp     = request.getParameter("message");
	int messageNum = 0;
	if(mnp!=null)
		messageNum = Integer.parseInt(mnp);
	String message = null;
	switch(messageNum){
		case 1  : message = "该分类已存在。";break;
		case 2  : message = "新建分类成功！";break;
		case 3  : message = "不存在的分类。";break;
		case 4  : message = "删除分类成功。";break;
		case 5  : message = "更新分类信息成功！";break;
		default : message = null;
	}
	
	String action  = request.getParameter("action");
	String catName = request.getParameter("cat");
	
%>
		<jsp:include page="header.jsp"></jsp:include>
		<jsp:include page="sidebar.jsp"></jsp:include>
		<script type="text/javascript">
			$(function(){
				// Accordion
				$("#accordion").accordion("activate",0);
				$("#post_categories").prepend(">");
				$("#submit").button();
			});
		</script>
		
		<%
		if(action!=null && action.equals("edit"))
		{
			if(catName==null || !CategoryService.isExist(catName))
				response.sendRedirect("/admin/categories.jsp?message=3");
			else {
				Category cat = CategoryService.getCategory(catName);
		%>
		<div id="content">
			<h2>Edit Category</h2>
			<hr/>
			<div id="editCategoryForm" style="float:left;padding:20px;width:100%"> 
				<form action="setcategory" method="post">
					<p style="margin:10px 20px">
						Category Name<br/>
						<input type="hidden" name="oname" value="<%=cat.getName() %>" />
						<input type="text" name="name" value="<%=cat.getName() %>" /><br/>
						Category Slug<br/>
						<input type="text" name="slug" value="<%=cat.getSlug() %>"/><br/>
						Description<br/>
						<textarea name="description" rows="5" cols="30"><%=cat.getDescription() %></textarea><br/>
						<input id="submit" type="submit" value="Update Category" /> 
					</p>
				</form>
			</div> 
		</div>
		<%
			}
		}else{
		
		%>
		<div id="content">
			<h2>Categories</h2>
			<hr/>
			<% if(message!=null){%>
			<div class="ui-widget" style="width:50em">
			<div class="ui-state-highlight ui-corner-all" style="margin-top: 20px; padding: 0 .7em;"> 
				<p><span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>
				<%= message %></p>
			</div>
			</div><br/>
			<%} 
			%>
			<div id="addCategoryForm" style="float:left;padding:10px;width:50%"> 
				<form action="addcategory" method="post">
					<h3>Add Category</h3>
					<p style="margin:10px 20px">
						Category Name<br/>
						<input type="text" name="name"/><br/>
						Category Slug<br/>
						<input type="text" name="slug"/><br/>
						Description<br/>
						<textarea name="description" rows="5" cols="30"></textarea><br/>
						<input id="submit" type="submit" value="add Category" /> 
					</p>
				</form>
			</div> 
			
			<div id="categoryList" class="ui-widget ui-widget-content ui-corner-all" 
			style="float:right;padding:10 30px;width:60%;"> 
			<h3 class="ui-widget-header" style="padding:5px 10px;font-size:12px;">Categories</h3>
				<table id="posts" border="1" cellspacing="0" style="margin:0;width:100%">
					<thead>
					<tr>
						<th>Name</th>
						<th>Description</th>
						<th>Slug</th>
						<th>Posts</th>
					</tr>
					</thead>
					<%
						List<Category> cats = CategoryService.getCategories();
						if(cats!=null)
						{
							for(Category cat:cats)
							{
								%>
					<tr>
						<td class="title_td"><a href="<%=cat.getEditLink() %>"><%= cat.getName() %> </a><div>&nbsp;<span class="postAction" style="display:none"><a href="<%=cat.getEditLink() %>">Edit</a>|<a href="<%=cat.getDeleteLink() %>">Delete</a></span></div></td>
						<td><%=cat.getDescription() %></td>
						<td><%=cat.getSlug() %></td>
						<td><%=cat.getPosts().size() %></td>
					</tr>
					
								<%
							}
						}
					%>
				</table>
			
			</div> 
		</div>
		<%
		}
		%>
		<jsp:include page="footer.jsp"></jsp:include>
